import { useGlobalState } from "@/stores/global";
import { storeToRefs } from "pinia";
import { watch } from "vue";

// 初始化微应用的props配置
export const initMicroAppStoreProps: Record<string, any> = {
  app1: {
    handleProps(app: any) {
      const store = useGlobalState();
      const { userInfo } = storeToRefs(store);
      
      // 监听全局状态变化并同步到子应用
      watch(userInfo, (newValue) => {
        if (app.instance) {
          app.instance.setGlobalState({ userInfo: newValue });
        }
      }, { deep: true });
      
      // 初始化时传递全局状态
      return {
        userInfo: userInfo.value,
        onChangeUserInfo: (newUserInfo: any) => {
          store.changeUserInfo(newUserInfo);
        }
      };
    }
  },
  app2: {
    handleProps(app: any) {
      const store = useGlobalState();
      const { userInfo } = storeToRefs(store);
      
      // 监听全局状态变化并同步到子应用
      watch(userInfo, (newValue) => {
        if (app.instance) {
          app.instance.setGlobalState({ userInfo: newValue });
        }
      }, { deep: true });
      
      // 初始化时传递全局状态
      return {
        userInfo: userInfo.value,
        onChangeUserInfo: (newUserInfo: any) => {
          store.changeUserInfo(newUserInfo);
        }
      };
    }
  }
};